package cn.yudao.order.service.impl;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.order.domain.OrderPetAcquisition;
import cn.yudao.order.domain.OrderPetAcquisitionLog;
import cn.yudao.order.domain.OrderProductAcquisition;
import cn.yudao.order.dto.AccomplishDto;
import cn.yudao.order.mapper.OrderPetAcquisitionLogMapper;
import cn.yudao.order.mapper.OrderProductAcquisitionMapper;
import cn.yudao.order.service.IOrderProductAcquisitionService;
import cn.yudao.basic.service.impl.BaseServiceImpl;
import cn.yudao.pet.domain.Pet;
import cn.yudao.pet.domain.SearchMasterMsg;
import cn.yudao.product.mapper.ProductPayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yudao
 * @since 2022-07-21
 */
@Service
public class OrderProductAcquisitionServiceImpl extends BaseServiceImpl<OrderProductAcquisition> implements IOrderProductAcquisitionService {

    @Autowired
    private OrderProductAcquisitionMapper orderProductAcquisitionMapper;

    @Autowired
    private ProductPayMapper productPayMapper;

    @Autowired
    private OrderPetAcquisitionLogMapper orderPetAcquisitionLogMapper;

    @Override
    public void confirm(Long id) {
        // 根据id查询到当前订单表对应的数据
        OrderProductAcquisition orderPetAcquisition = orderProductAcquisitionMapper.loadById(id);
        // 设置订单状态为null
        orderPetAcquisition.setState(-1);
        orderPetAcquisition.setUserId(null);
        orderProductAcquisitionMapper.upadte(orderPetAcquisition);
        // 我们先根据订单中的msgid查询到对应的寻主信息msg
        Long tProductPayId = orderPetAcquisition.getTProductPayId();
        productPayMapper.removeById(tProductPayId);
        // 删除寻主信息的shopid -- 放入寻主池中
        extracted(0,orderPetAcquisition);
    }

    private void extracted(Integer state,OrderProductAcquisition orderPetAcquisition) {
        Long id = orderPetAcquisition.getId();
        OrderPetAcquisitionLog orderPetAcquisitionLog = new OrderPetAcquisitionLog();
        orderPetAcquisitionLog.setAcqId(id);
        orderPetAcquisitionLog.setAuditTime(new Date());
        orderPetAcquisitionLog.setState(state);
        if (state == 0){
            orderPetAcquisitionLog.setNote("订单失败");
        }else if (state == 1){
            orderPetAcquisitionLog.setNote("完成订单");
        }
        orderPetAcquisitionLogMapper.save(orderPetAcquisitionLog);
    }

    @Override
    public void complete(AccomplishDto accomplishDto) {
        // 非空判断
        Long id = accomplishDto.getId();
//        BigDecimal money = accomplishDto.getMoney();
//        Integer payType = accomplishDto.getPayType();
//        if (id == null || money == null || payType == null){
//            throw new BusinessException("请正确填写参数");
//        }
        // 获取当前的订单
        OrderProductAcquisition orderPetAcquisition = orderProductAcquisitionMapper.loadById(id);
        // 修改当前订单的状态
        orderPetAcquisition.setState(2);
        orderProductAcquisitionMapper.upadte(orderPetAcquisition);
        // 新增一个订单成功的日志表
        extracted(1,orderPetAcquisition);
    }
}
